home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / EXGRAF3D / BOXSPHER.C < prev    next >
C/C++ Source or Header  |  1990-09-27  |  3KB  |  166 lines

  1. /*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ÑÑÑ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
  2. /*                                                                               */
  3. /* BoxSphere.c    --    Translation of Pascal 'LISA/EXAMPLE/BOXSPHERE.TEXT'        */
  4. /*                    from Apple Programmer's and Developer's Association        */
  5. /*                    disk 'Macintosh Example Applications and Sources v.1.0'    */
  6. /*                                                                               */
  7. /*                    Translation to LightSpeed C by Lewis E. Garrett - 9/27/90  */
  8. /*                                                   CIS 71147,2202               */
  9. /*                                                                               */
  10. /*                                                                               */
  11. /*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ÑÑÑ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
  12.  
  13. #include "Graf3D.h"
  14.  
  15. /* CONST */
  16. #define keyOrMouse mDownMask+keyDownMask
  17. #define NIL 0L
  18.  
  19. /*    GrafPort gPort; */
  20. Port3D         gPort3D;
  21. Fixed sinTable[31]; /*24 steps, 15 degree increments*/
  22. EventRecord dummy;
  23. Rect         inRect;
  24. WindowPtr     w;
  25. Port3D        *ptr3D;
  26.  
  27. main()
  28.  
  29. { /* main program */
  30.     FlushEvents(everyEvent, 0);
  31.     InitGraf(&thePort);
  32.     ptr3D    = &gPort3D;
  33.     InitGrf3D(&ptr3D);
  34.     InitFonts();
  35.     InitWindows();
  36.     InitMenus();
  37.     TEInit();
  38.     InitDialogs(NIL);
  39.  
  40.     InitCursor();
  41.     HideCursor();
  42.  
  43.     w = NewWindow(NIL, &screenBits.bounds, "\p", TRUE, 2, (WindowPtr)(-1L), FALSE, 0L);
  44.     SetPort(w);
  45.  
  46.     PenPat(white);
  47.     BackPat(black);
  48.  
  49.     EraseRect(&thePort->portRect);
  50.  
  51.     Draw3D();
  52.  
  53.     ShowCursor();
  54.     DisposeWindow(w);
  55. }
  56.  
  57.  
  58. Draw3D()
  59. {
  60.     Open3DPort(&gPort3D);
  61.     InitSinTable();
  62.  
  63.     FrameRect(&thePort->portRect);
  64.     inRect = thePort->portRect;
  65.     InsetRect(&inRect, 1, 1);
  66.     LookAt(-983040, 655360, 983040, -655360); /*LookAt (-15, 10, 15, -10)*/
  67.     ViewAngle(1966080); /*ViewAngle(30)*/
  68.     Roll(1310720); /*Roll(20)*/
  69.     Pitch(4915200); /*Pitch(75)*/
  70.  
  71.     do    {
  72.         EraseRect(&inRect);
  73.         Yaw(327680);            /*Yaw(5)*/
  74.         DrawCube(1048576);        /*DrawCube(16)*/
  75.         DrawSphere(524288);     /*DrawSphere(8)*/
  76.     }
  77.     while (!GetOSEvent(keyOrMouse, &dummy));
  78. }
  79.  
  80. InitSinTable()
  81. {
  82.     /* CONST */
  83. #define  f15DivRad 17157L
  84.     int iAng;
  85.  
  86.     for ( iAng = 0; iAng<=30; iAng++ )      {
  87.         sinTable[iAng] = FixDiv(FracSin(FixMul(FixRatio(iAng, 1),
  88.         f15DivRad)), 1073741824);
  89.  
  90.     }
  91. }
  92.  
  93. DrawCube(Edge)
  94. Fixed Edge;
  95. {
  96.     Fixed Half;
  97.  
  98.     Half = FixDiv (Edge, 131072);     /*Half := Edge / 2*/
  99.     Move3D(-Half,-Half,-Half);
  100.     Line3D(0,Edge,0);
  101.     Line3D(Edge,0,0);
  102.     Line3D(0,-Edge,0);
  103.     Line3D(-Edge,0,0);
  104.  
  105.     Move3D(0,0,Edge);
  106.     Line3D(0,Edge,0);
  107.     Line3D(Edge,0,0);
  108.     Line3D(0,-Edge,0);
  109.     Line3D(-Edge,0,0);
  110.  
  111.     Move3D(0,0,-Edge);
  112.     Line3D(0,0,Edge);
  113.     Move3D(0,Edge,-Edge);
  114.     Line3D(0,0,Edge);
  115.     Move3D(Edge,0,-Edge);
  116.     Line3D(0,0,Edge);
  117.     Move3D(0,-Edge,-Edge);
  118.     Line3D(0,0,Edge);
  119.  
  120.     Move3D(-Half,Half,-Half);
  121. }
  122.  
  123. Circle(radius)
  124. Fixed radius;
  125.  
  126. {
  127.     Fixed xCenter,
  128.     yCenter,
  129.     ang;
  130.     int iAng;
  131.  
  132.     xCenter = gPort3D.pen.x;
  133.     yCenter = gPort3D.pen.y;
  134.     MoveTo2D(xCenter, yCenter+radius);
  135.     for ( iAng = 0 ; iAng<=24; iAng++ )
  136.         LineTo2D(xCenter + FixMul(radius, sinTable[iAng]),
  137.         yCenter + FixMul(radius, sinTable[iAng + 6]));
  138.     MoveTo2D(xCenter, yCenter);
  139. }
  140.  
  141. DrawSphere(radius)
  142. Fixed radius;
  143.  
  144. {
  145.     int zPlane;
  146.     Fixed zStep,
  147.     dz;
  148.     Point3D center;
  149.  
  150.     /* WITH gPort3D */
  151.     center = gPort3D.pen;
  152.     zStep = FixDiv(radius, 524288);  /*zStep := radius / 8;*/
  153.     for ( zPlane = -7 ; zPlane<=7; zPlane++ )
  154.     {
  155.         dz = FixMul(zStep, FixRatio(zPlane, 1));
  156.         MoveTo3D(center.x, center.y, center.z + dz); /*Get on the right plane.*/
  157.         Circle(FracSqrt(FixMul(radius, radius) - FixMul(dz, dz)) >> 7);
  158.  
  159.         /*Draw a circle there.*/
  160.     }
  161.     MoveTo3D(center.x, center.y, center.z);
  162. }
  163.  
  164.  
  165.  
  166.